package com.cskaoyan.service.wxservice.impl;

import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.service.wxservice.AddressWeChatService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 小黑球
 * @datetime 2023/1/1 23:42
 * @description: TODO:
 */
@Service
public class AddressWeChatServiceImpl implements AddressWeChatService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> list() {
        // example做的是要拼接一些条件
        MarketAddressExample example = new MarketAddressExample();
        // 在criteria中可以拼接多个条件
        MarketAddressExample.Criteria criteria = example.createCriteria();
        // 该接口的拼接条件
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> list = marketAddressMapper.selectByExample(example);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public void delete(MarketAddress marketAddress) {
        marketAddress.setDeleted(true);
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
    }

    @Override
    public Integer save(MarketAddress marketAddress) {
        marketAddress.setAddTime(new Date());
        if (marketAddress.getId() == 0) {
            // 如果id不存在,执行插入操作
            marketAddressMapper.insertSelective(marketAddress);
        } else {
            // 如果id存在执行update操作
            marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        }
        return marketAddress.getId();
    }

    @Override
    public MarketAddress detail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }


}
